﻿Imports System.IO
Module dism

    Function help()
        Dim LastOutput As String = ""
        Dim dism As New Process
        With dism.StartInfo
            .RedirectStandardError = True
            .RedirectStandardOutput = True
            .UseShellExecute = False
            .CreateNoWindow = True
            .FileName = "dism.exe"
            .Arguments = "/?"
        End With
        dism.Start()
        While Not dism.StandardOutput.EndOfStream

            LastOutput = ""
            For i = 1 To 16
                LastOutput += vbCrLf & dism.StandardOutput.ReadLine
            Next
            MsgBox(LastOutput)

        End While


        dism.WaitForExit()

        Return dism.ExitCode

    End Function

    Function GetMountedWimInfo()





        'Return parse_MountedWimInfo(proc_get_mountedWimInfo())
        Return parse_WimInfo(proc_get_WimInfo("C:\Users\jleitsch\Desktop\install.wim"))

    End Function

    Function GetWimInfo(ByVal wimfile As String, ByVal index As Integer) ' As WimInfo
        Dim LastOutput As String = ""
        Dim mountList As New List(Of MountedImage)
        Dim dism As New Process
        With dism.StartInfo
            .RedirectStandardError = True
            .RedirectStandardOutput = True
            .UseShellExecute = False
            .CreateNoWindow = True
            .FileName = "dism.exe"
            .Arguments = "/Get-MountedWimInfo"
        End With
        dism.Start()
        While Not dism.StandardOutput.EndOfStream
            LastOutput = dism.StandardOutput.ReadLine
            If InStr(LastOutput, "Mount Dir") Then
                Dim mount As New MountedImage
                mount.MountedDir = Split(LastOutput, " : ")(1)
                mount.ImageFile = Split(dism.StandardOutput.ReadLine, " : ")(1)
                mount.ImageIndex = Split(dism.StandardOutput.ReadLine, " : ")(1)
                mount.MountedRW = Split(dism.StandardOutput.ReadLine, " : ")(1)
                mount.Status = Split(dism.StandardOutput.ReadLine, " : ")(1)
                mountList.Add(mount)
            End If
        End While
        dism.WaitForExit()


        'Dim LastOutput As String() = Nothing
        'Dim mountList As New List(Of MountedImage)
        'Dim dism As New Process
        With dism.StartInfo
            .RedirectStandardError = True
            .RedirectStandardOutput = True
            .UseShellExecute = False
            .CreateNoWindow = True
            .FileName = "dism.exe"
            .Arguments = "/Get-WimInfo "
            .Arguments += "/wimfile:"""
            .Arguments += wimfile
            .Arguments += """ /index:"
            .Arguments += index.ToString
        End With
        dism.Start()
        Dim info ' As New WimInfo
        While Not dism.StandardOutput.EndOfStream
            'LastOutput = Split(dism.StandardOutput.ReadLine, " : ")
            Select Case LastOutput(0)
                Case "Index"
                    info.index = LastOutput(1)
                Case "Name"
                    info.name = LastOutput(1)
                Case "Description"
                    info.description = LastOutput(1)
                Case "Size"
                    info.size = LastOutput(1)
                Case "Architecture"
                    info.architecture = LastOutput(1)
                Case "Hal"
                    info.hal = LastOutput(1)
                Case "Version"
                    info.version = LastOutput(1)
                Case "ServicePack Build"
                    info.spBuild = LastOutput(1)
                Case "ServicePack Level"
                    info.spLevel = LastOutput(1)
                Case "Edition"
                    info.edition = LastOutput(1)
                Case "Installation"
                    info.installation = LastOutput(1)
                Case "ProductType"
                    info.productType = LastOutput(1)
                Case "ProductSuite"
                    info.productSuite = LastOutput(1)
                Case "System Root"
                    info.systemRoot = LastOutput(1)
                Case "Directories"
                    info.directories = LastOutput(1)
                Case "Files"
                    info.files = LastOutput(1)
                Case "Created"
                    info.created = LastOutput(1)
                Case "Modified"
                    info.modified = LastOutput(1)
            End Select

        End While



        dism.WaitForExit()
        Return info
    End Function



    Class MountedImage2
        Private _mountedDir As String
        Public Property MountedDir() As String
            Get
                Return _mountedDir
            End Get
            Set(ByVal value As String)
                _mountedDir = value
            End Set
        End Property
        Private _imageFile As String
        Public Property ImageFile() As String
            Get
                Return _imageFile
            End Get
            Set(ByVal value As String)
                _imageFile = value
            End Set
        End Property
        Private _imageIndex As String
        Public Property ImageIndex() As String
            Get
                Return _imageIndex
            End Get
            Set(ByVal value As String)
                _imageIndex = value
            End Set
        End Property
        Private _mountedRW As String
        Public Property MountedRW() As String
            Get
                Return _mountedRW
            End Get
            Set(ByVal value As String)
                _mountedRW = value
            End Set
        End Property
        Private _status As String
        Public Property Status() As String
            Get
                Return _status
            End Get
            Set(ByVal value As String)
                _status = value
            End Set
        End Property
    End Class

    Class WimInfo2
        Private _index As String
        Public Property index() As String
            Get
                Return _index
            End Get
            Set(ByVal value As String)
                _index = value
            End Set
        End Property
        Private _name As String
        Public Property name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property
        Private _description As String
        Public Property description() As String
            Get
                Return _description
            End Get
            Set(ByVal value As String)
                _description = value
            End Set
        End Property
        Private _size As String
        Public Property size() As String
            Get
                Return _size
            End Get
            Set(ByVal value As String)
                _size = value
            End Set
        End Property
        Private _architecture As String
        Public Property architecture() As String
            Get
                Return _architecture
            End Get
            Set(ByVal value As String)
                _architecture = value
            End Set
        End Property
        Private _hal As String
        Public Property hal() As String
            Get
                Return _hal
            End Get
            Set(ByVal value As String)
                _hal = value
            End Set
        End Property
        Private _version As String
        Public Property version() As String
            Get
                Return _version
            End Get
            Set(ByVal value As String)
                _version = value
            End Set
        End Property
        Private _spBuild As String
        Public Property spBuild() As String
            Get
                Return _spBuild
            End Get
            Set(ByVal value As String)
                _spBuild = value
            End Set
        End Property
        Private _spLevel As String
        Public Property spLevel() As String
            Get
                Return _spLevel
            End Get
            Set(ByVal value As String)
                _spLevel = value
            End Set
        End Property
        Private _edition As String
        Public Property edition() As String
            Get
                Return _edition
            End Get
            Set(ByVal value As String)
                _edition = value
            End Set
        End Property
        Private _installation As String
        Public Property installation() As String
            Get
                Return _installation
            End Get
            Set(ByVal value As String)
                _installation = value
            End Set
        End Property
        Private _productType As String
        Public Property productType() As String
            Get
                Return _productType
            End Get
            Set(ByVal value As String)
                _productType = value
            End Set
        End Property
        Private _productSuite As String
        Public Property productSuite() As String
            Get
                Return _productSuite
            End Get
            Set(ByVal value As String)
                _productSuite = value
            End Set
        End Property
        Private _systemRoot As String
        Public Property systemRoot() As String
            Get
                Return _systemRoot
            End Get
            Set(ByVal value As String)
                _systemRoot = value
            End Set
        End Property
        Private _directories As String
        Public Property directories() As String
            Get
                Return _directories
            End Get
            Set(ByVal value As String)
                _directories = value
            End Set
        End Property
        Private _files As String
        Public Property files() As String
            Get
                Return _files
            End Get
            Set(ByVal value As String)
                _files = value
            End Set
        End Property
        Private _created As String
        Public Property created() As String
            Get
                Return _created
            End Get
            Set(ByVal value As String)
                _created = value
            End Set
        End Property
        Private _modified As String
        Public Property modified() As String
            Get
                Return _modified
            End Get
            Set(ByVal value As String)
                _modified = value
            End Set
        End Property
        Private _languages As String()
        Public Property languages() As String()
            Get
                Return _languages
            End Get
            Set(ByVal value As String())
                _languages = value
            End Set
        End Property
    End Class


#Region "Processes"

#Region "DISM"
    Private Function proc_get_mountedWimInfo()
        Dim dism As New Process
        With dism.StartInfo
            .RedirectStandardError = True
            .RedirectStandardOutput = True
            .UseShellExecute = False
            .CreateNoWindow = True
            .FileName = "dism.exe"
            .Arguments = "/Get-MountedWimInfo"
        End With
        dism.Start()
        dism.WaitForExit()
        Return dism.StandardOutput.ReadToEnd
    End Function
    Private Function proc_get_WimInfo(ByVal wimfile As String, Optional ByVal index As String = "")
        Dim dism As New Process
        With dism.StartInfo
            .RedirectStandardError = True
            .RedirectStandardOutput = True
            .UseShellExecute = False
            .CreateNoWindow = True
            .FileName = "dism.exe"
            .Arguments = "/Get-WimInfo"
            .Arguments += " /wimfile:"""
            .Arguments += wimfile
            If Not index = "" Then
                .Arguments += """ /index:"
                .Arguments += index
            End If
        End With
        dism.Start()
        dism.WaitForExit()
        Return dism.StandardOutput.ReadToEnd
    End Function
    Private Function proc_Commit_Wim(ByVal MountDir As String)
        Dim dism As New Process
        With dism.StartInfo
            .RedirectStandardError = True
            .RedirectStandardOutput = True
            .UseShellExecute = False
            .CreateNoWindow = True
            .FileName = "dism.exe"
            .Arguments = "/Commit-Wim"
            .Arguments += " /MountDir:"""
            .Arguments += MountDir
            .Arguments += """"
        End With
        dism.Start()
        dism.WaitForExit()
        Return dism.StandardOutput.ReadToEnd
    End Function
    Private Function proc_Unmount_Wim(ByVal MountDir As String, ByVal commit As Boolean)
        Dim dism As New Process
        With dism.StartInfo
            .RedirectStandardError = True
            .RedirectStandardOutput = True
            .UseShellExecute = False
            .CreateNoWindow = True
            .FileName = "dism.exe"
            .Arguments = "/Unmount-Wim"
            .Arguments += " /MountDir:"""
            .Arguments += MountDir
            .Arguments += """ /"
            If commit Then .Arguments += "commit" Else .Arguments += "discard"
        End With
        dism.Start()
        dism.WaitForExit()
        Return dism.StandardOutput.ReadToEnd
    End Function
    Private Function proc_Mount_Wim(ByVal wimfile As String, ByVal index As String, ByVal MountDir As String, Optional ByVal read_only As Boolean = False)

        Dim dism As New Process
        With dism.StartInfo
            .RedirectStandardError = True
            .RedirectStandardOutput = True
            .UseShellExecute = False
            .CreateNoWindow = True
            .FileName = "dism.exe"
            .Arguments = "/Mount-Wim"
            .Arguments += " /wimfile:"""
            .Arguments += wimfile
            .Arguments += """ /index:"
            .Arguments += index
            .Arguments += " /MountDir:"""
            .Arguments += MountDir
            .Arguments += """"
            If read_only Then .Arguments += " /readonly"
        End With
        dism.Start()
        dism.WaitForExit()
        Return dism.StandardOutput.ReadToEnd
    End Function
    Private Function proc_Remount_Wim(ByVal MountDir As String)
        Dim dism As New Process
        With dism.StartInfo
            .RedirectStandardError = True
            .RedirectStandardOutput = True
            .UseShellExecute = False
            .CreateNoWindow = True
            .FileName = "dism.exe"
            .Arguments = "/Remount-Wim"
            .Arguments += " /MountDir:"""
            .Arguments += MountDir
            .Arguments += """"
        End With
        dism.Start()
        dism.WaitForExit()
        Return dism.StandardOutput.ReadToEnd
    End Function
    Private Function proc_Cleanup_Wim()
        Dim dism As New Process
        With dism.StartInfo
            .RedirectStandardError = True
            .RedirectStandardOutput = True
            .UseShellExecute = False
            .CreateNoWindow = True
            .FileName = "dism.exe"
            .Arguments = "/Cleanup-Wim"
        End With
        dism.Start()
        dism.WaitForExit()
        Return dism.StandardOutput.ReadToEnd
    End Function

    Private Function parse_MountedWimInfo(ByVal info As String)
        Dim indexCount = (System.Text.RegularExpressions.Regex.Matches(info, "Image Index").Count)
        If indexCount > 0 Then
            Dim indexList As New List(Of MountedImage)
            Dim reader As StringReader = New StringReader(info)

            While Not reader.Peek = -1
                Dim sLine() = Split(reader.ReadLine, " : ")
                If InStr(sLine(0), "Mount Dir") Then
                    Dim index As New MountedImage
                    index.MountedDir = sLine(1)
                    index.ImageFile = Split(reader.ReadLine, " : ")(1)
                    index.ImageIndex = Split(reader.ReadLine, " : ")(1)
                    index.MountedRW = Split(reader.ReadLine, " : ")(1)
                    index.Status = Split(reader.ReadLine, " : ")(1)
                    indexList.Add(index)
                End If
            End While
            Return indexList
        End If
        Return 0
    End Function
    Private Function parse_WimInfo(ByVal info As String)
        Dim indexCount = (System.Text.RegularExpressions.Regex.Matches(info, "Index :").Count)
        If indexCount > 0 Then
            Dim indexList As New List(Of WimInfo)
            Dim reader As StringReader = New StringReader(info)
            While Not reader.Peek = -1
                Dim sLine() = Split(reader.ReadLine, " : ")
                If InStr(sLine(0), "Index") Then
                    Dim index As New WimInfo
                    index.index = sLine(1)
                    index.name = Split(reader.ReadLine, " : ")(1)
                    index.description = Split(reader.ReadLine, " : ")(1)
                    index.size = Split(reader.ReadLine, " : ")(1)
                    indexList.Add(index)
                End If
            End While
            Return indexList
        End If
        Return 0
    End Function
    Private Function parse_IndexInfo(ByVal info As String)
        Dim index As New IndexInfo
        Dim reader As StringReader = New StringReader(info)
        While Not reader.Peek = -1
            Dim sLine() = Split(reader.ReadLine, " : ")
            Select Case sLine(0)
                Case "Index"
                    index.index = sLine(1)
                Case "Name"
                    index.name = sLine(1)
                Case "Description"
                    index.description = sLine(1)
                Case "Size"
                    index.size = sLine(1)
                Case "Architecture"
                    index.architecture = sLine(1)
                Case "Hal"
                    index.hal = sLine(1)
                Case "Version"
                    index.version = sLine(1)
                Case "ServicePack Build"
                    index.spBuild = sLine(1)
                Case "ServicePack Level"
                    index.spLevel = sLine(1)
                Case "Edition"
                    index.edition = sLine(1)
                Case "Installation"
                    index.installation = sLine(1)
                Case "ProductType"
                    index.productType = sLine(1)
                Case "ProductSuite"
                    index.productSuite = sLine(1)
                Case "System Root"
                    index.systemRoot = sLine(1)
                Case "Directories"
                    index.directories = sLine(1)
                Case "Files"
                    index.files = sLine(1)
                Case "Created"
                    index.created = sLine(1)
                Case "Modified"
                    index.modified = sLine(1)
            End Select
        End While
        Return index
    End Function

    Private Structure MountedImage
        Private _mountedDir As String
        Public Property MountedDir() As String
            Get
                Return _mountedDir
            End Get
            Set(ByVal value As String)
                _mountedDir = value
            End Set
        End Property
        Private _imageFile As String
        Public Property ImageFile() As String
            Get
                Return _imageFile
            End Get
            Set(ByVal value As String)
                _imageFile = value
            End Set
        End Property
        Private _imageIndex As String
        Public Property ImageIndex() As String
            Get
                Return _imageIndex
            End Get
            Set(ByVal value As String)
                _imageIndex = value
            End Set
        End Property
        Private _mountedRW As String
        Public Property MountedRW() As String
            Get
                Return _mountedRW
            End Get
            Set(ByVal value As String)
                _mountedRW = value
            End Set
        End Property
        Private _status As String
        Public Property Status() As String
            Get
                Return _status
            End Get
            Set(ByVal value As String)
                _status = value
            End Set
        End Property

    End Structure
    Private Structure IndexInfo
        Private _index As String
        Public Property index() As String
            Get
                Return _index
            End Get
            Set(ByVal value As String)
                _index = value
            End Set
        End Property
        Private _name As String
        Public Property name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property
        Private _description As String
        Public Property description() As String
            Get
                Return _description
            End Get
            Set(ByVal value As String)
                _description = value
            End Set
        End Property
        Private _size As String
        Public Property size() As String
            Get
                Return _size
            End Get
            Set(ByVal value As String)
                _size = value
            End Set
        End Property
        Private _architecture As String
        Public Property architecture() As String
            Get
                Return _architecture
            End Get
            Set(ByVal value As String)
                _architecture = value
            End Set
        End Property
        Private _hal As String
        Public Property hal() As String
            Get
                Return _hal
            End Get
            Set(ByVal value As String)
                _hal = value
            End Set
        End Property
        Private _version As String
        Public Property version() As String
            Get
                Return _version
            End Get
            Set(ByVal value As String)
                _version = value
            End Set
        End Property
        Private _spBuild As String
        Public Property spBuild() As String
            Get
                Return _spBuild
            End Get
            Set(ByVal value As String)
                _spBuild = value
            End Set
        End Property
        Private _spLevel As String
        Public Property spLevel() As String
            Get
                Return _spLevel
            End Get
            Set(ByVal value As String)
                _spLevel = value
            End Set
        End Property
        Private _edition As String
        Public Property edition() As String
            Get
                Return _edition
            End Get
            Set(ByVal value As String)
                _edition = value
            End Set
        End Property
        Private _installation As String
        Public Property installation() As String
            Get
                Return _installation
            End Get
            Set(ByVal value As String)
                _installation = value
            End Set
        End Property
        Private _productType As String
        Public Property productType() As String
            Get
                Return _productType
            End Get
            Set(ByVal value As String)
                _productType = value
            End Set
        End Property
        Private _productSuite As String
        Public Property productSuite() As String
            Get
                Return _productSuite
            End Get
            Set(ByVal value As String)
                _productSuite = value
            End Set
        End Property
        Private _systemRoot As String
        Public Property systemRoot() As String
            Get
                Return _systemRoot
            End Get
            Set(ByVal value As String)
                _systemRoot = value
            End Set
        End Property
        Private _directories As String
        Public Property directories() As String
            Get
                Return _directories
            End Get
            Set(ByVal value As String)
                _directories = value
            End Set
        End Property
        Private _files As String
        Public Property files() As String
            Get
                Return _files
            End Get
            Set(ByVal value As String)
                _files = value
            End Set
        End Property
        Private _created As String
        Public Property created() As String
            Get
                Return _created
            End Get
            Set(ByVal value As String)
                _created = value
            End Set
        End Property
        Private _modified As String
        Public Property modified() As String
            Get
                Return _modified
            End Get
            Set(ByVal value As String)
                _modified = value
            End Set
        End Property
        Private _languages As String()
        Public Property languages() As String()
            Get
                Return _languages
            End Get
            Set(ByVal value As String())
                _languages = value
            End Set
        End Property
    End Structure
    Private Structure WimInfo
        Private _index As String
        Property index() As String
            Get
                Return _index
            End Get
            Set(ByVal value As String)
                _index = value
            End Set
        End Property
        Private _name As String
        Public Property name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property
        Private _description As String
        Public Property description() As String
            Get
                Return _description
            End Get
            Set(ByVal value As String)
                _description = value
            End Set
        End Property
        Private _size As String
        Public Property size() As String
            Get
                Return _size
            End Get
            Set(ByVal value As String)
                _size = value
            End Set
        End Property
    End Structure

#End Region

#Region "ImageX"

#End Region

#End Region

End Module
